//单向链表
function LinkList() {
  let p = LinkList.prototype;
  this.head = null;
  this.length = 0;
  function Node(data) {
    this.next = null;
    this.data = data;
  }
  p.toString = function() {
    if (this.head == null) return null;
    else {
      let arr = [];
      let current = this.head;
      while (current) {
        arr.push(current);
        current = current.next;
      }
      return arr;
    }
  };
  p.size = function() {
    return this.length;
  };
  p.isEmpty = function() {
    return this.head === null;
  };
  p.get = function(position) {
    let current = this.head;
    let i = 0;
    while (current !== null) {
      if (position == i) {
        return current;
      }
      current = current.next;
      i++;
    }
  };
  p.indexOf = function(data) {
    let current = this.head;
    let i = 0;
    while (current !== null) {
      if (data == current.data) {
        return i;
      }
      current = current.next;
      i++;
    }
  };
  p.update = function(position, data) {
    let current = this.head;
    let i = 0;
    while (current !== null) {
      if (position == i) {
        current.data = data;
        break;
      }
      current = current.next;
      i++;
    }
  };
  p.append = function(data) {
    let node = new Node(data);
    if (this.head === null) this.head = node;
    else {
      let current = this.head;
      while (current.next !== null) {
        current = current.next;
      }
      current.next = node;
    }
    this.length++;
  };
  p.insert = function(position, data) {
    let node = new Node(data);
    if (this.head === null) this.head = node;
    else {
      let front = null;
      let current = this.head;
      let i = 0;
      while (current !== null) {
        if (position == i) {
          if (front == null) {
            this.head = node;
            node.next = current;
          } else {
            front.next = node;
            node.next = current;
          }
          break;
        }
        front = current;
        current = current.next;
        i++;
      }
    }
    this.length++;
  };
  p.removeAt = function(position) {
    let front = null;
    let current = this.head;
    let i = 0;
    while (current !== null) {
      if (position == i) {
        if (front && front.next) {
          front.next = current.next;
        } else {
          this.head = current.next;
        }
        break;
      }
      front = current;
      current = current.next;
      i++;
    }
    this.length--;
  };
  p.remove = function(data) {
    let front = null;
    let current = this.head;
    while (current !== null) {
      if (data == current.data) {
        if (front && front.next) {
          front.next = current.next;
        } else {
          this.head = current.next;
        }
        break;
      }
      front = current;
      current = current.next;
    }
    this.length--;
  };
}
let ll = new LinkList();
ll.append(1);
ll.append(2);
ll.append(3);
ll.append(4);
ll.append(5);
console.log(ll.size());
console.log(ll.toString());
ll.insert(2, 100);
console.log(ll.toString());
ll.update(2, 80);
console.log(ll.toString());
ll.insert(1, 999);
console.log(ll.toString());
console.log(ll.size());
console.log(ll.isEmpty());
console.log(ll.get(5));
ll.removeAt(0);
console.log(ll.toString());
ll.removeAt(5);
console.log(ll.toString());
ll.remove(999);
console.log(ll.toString());
ll.remove(4);
console.log(ll.toString());
